package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.TGoods;
import com.ruoyi.system.domain.TestCourse;
import com.ruoyi.system.service.TGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/system/goods")
public class TGoodsController extends BaseController {
    @Autowired
    private TGoodsService goodsService;

    /*商品分页查询*/
    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(TGoods goods)
    {
        //分页查询，PageHelper.startPage
        startPage();
        //将查询到的数据封装到list
        List<TGoods> list = goodsService.selectGoodsList(goods);
        //封装数据
        return getDataTable(list);
    }
    /*条件查询，*/
    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable Long goodsId){
        return success(goodsService.selectGoodsById(goodsId));
    }

    /*增加操作
     * @RequestBody将请求报文转换为对象，
     * */
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TGoods goods)
    {
        //校验商品是否唯一
        if (!goodsService.checkGoodsNameUnique(goods))
        {
            return error("新增'" + goods.getGoodsName() + "'失败，名称已存在");
        }

            return toAjax(goodsService.insertGoods(goods));
    }

    /*修改操作*/
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TGoods goods)
    {


        System.out.println("goodsId="+goods.getGoodsId());
        return toAjax(goodsService.updateGoods(goods));
    }
    /*删除操作，前端传递id来进行操作*/
    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        return toAjax(goodsService.deleteGoodsByIds(goodsIds));
    }

    /**
     * 图片上传
     */
    @PostMapping("/upload")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file) throws IOException, InvalidExtensionException {
        if (!file.isEmpty()){
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION, true);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", avatar);
            return ajax;
        }
        return error("图片上传异常");
    }

    //获取商品数据，获取商品表
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:goods:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TGoods goods)
    {
        //查询列表信息
        List<TGoods> list = goodsService.selectGoodsList(goods);
        //商品信息处理
        ExcelUtil<TGoods> util = new ExcelUtil<TGoods>(TGoods.class);
        util.exportExcel(response, list, "商品数据","商品数据");
        System.out.println("无异常，导出数据成功");
    }
}
